from django.shortcuts import render

# Create your views here.
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from community.models import Article, Comment
from community.serializers import ArticleSerializers, CommentSerializers
from utils.permission import auto_user, update_auto_user, destroy_auto_user


class ArticlePaginationPageNumber(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'size'
    max_page_size = 100
class ArticleViewSet(ModelViewSet):
    queryset = Article.objects.filter(is_delete=False)
    serializer_class = ArticleSerializers
    permission_classes = [IsAuthenticated]
    pagination_class = ArticlePaginationPageNumber
    def get_queryset(self):
        #这段代码的意思就是加入访问的状态是list或者retrieve的时候，就返回下面这个查询
        #至于为什么不在上面直接写，因该是怕影响其他方法，毕竟保存的时候也要做操作
        if self.action in ['list','retrieve']:
            return Article.objects.filter(is_delete=False,status=1)
        return self.queryset
    @auto_user
    def create(self, request, *args, **kwargs):
        return ModelViewSet.create(self, request, *args, **kwargs)
    @action(methods=['put'],detail=True)
    def update_commentcount(self,request,pk):
        try:
            article = self.get_queryset().get(id=pk)
        except:
            return Response({
                'status':status.HTTP_404_NOT_FOUND,
                'msg': '无法查询到该文章的信息！'
                })
        article.page_view+=1
        print(article.page_view)
        article.save()
        return Response({
            'msg':'ok'
        })
    @update_auto_user
    def update(self, request, *args, **kwargs):
        return ModelViewSet.update(self, request, *args, **kwargs)
    @destroy_auto_user
    def destroy(self, request, *args, **kwargs):
        return ModelViewSet.destroy(self, request, *args, **kwargs)

class CommentViewSet(ModelViewSet):
    queryset = Comment.objects.filter(is_delete=False)
    serializer_class = CommentSerializers
    permission_classes = [IsAuthenticated]
    @auto_user
    def create(self, request, *args, **kwargs):
        return ModelViewSet.create(self, request, *args, **kwargs)

    @destroy_auto_user
    def destroy(self, request, *args, **kwargs):
        return ModelViewSet.destroy(self, request, *args, **kwargs)
    @action(methods=['get'],detail=True)
    def get_article_commnet(self,request,pk):
        try:
            #关联字段加上__加上关联表的字段，就可以自动的锁定到 然后在进行筛选
            comment = self.get_queryset().filter(article__id = pk)
        except:
            return Response({
                'status': status.HTTP_404_NOT_FOUND,
                'msg': '无法查询到该文章的评论信息！'
            })
        #进行反序列化
        serializer = self.get_serializer(comment,many=True)
        return Response({
            'status': status.HTTP_201_CREATED,
            'data': serializer.data
        })


